package com.telecom.llyc.front;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.telecom.llyc.db.dac.Dac;
import com.telecom.llyc.db.dac.Paging;
import com.telecom.llyc.db.model.Doc;

@WebServlet("/front/ListFile")
public class ListFile extends HttpServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String pageNoStr = request.getParameter("pageNo");
		if (pageNoStr == null)
			pageNoStr = "0";
		int pageNo = Integer.parseInt(pageNoStr);

		String pageSizeStr = request.getParameter("pageSize");
		if (pageSizeStr == null)
			pageSizeStr = "10";
		int pageSize = Integer.parseInt(pageSizeStr);

		if (request.getParameter("lastPage") != null)
			pageNo = Integer.MAX_VALUE;
		if (request.getParameter("firstPage") != null)
			pageNo = 0;
		if (request.getParameter("previousPage") != null)
			pageNo--;
		if (request.getParameter("nextPage") != null)
			pageNo++;

		List<Doc> docList = null;
		//User curUser = (User) request.getSession().getAttribute("curUser");
		//if (curUser.getId() == 0)
			docList = Dac.getInstance().allDocs();
		//else
		//	docList = Dac.getInstance().getDocsOfUser(curUser.getId());

			String physicalPath = getServletContext().getRealPath("/WEB-INF/file");
			
			System.out.println(physicalPath);
			
		Paging<Doc> paging = new Paging<Doc>(pageSize, pageNo, docList);
		request.setAttribute("paging", paging);
		request.getRequestDispatcher("/WEB-INF/jsp/front/ListDoc.jsp").forward(request, response);

	}
}
